package com.after.saas.common.constants;

/**
 * @Classname JwtEnum
 * @Date 2021/5/7 15:45
 * @Created by hjchen
 */
public enum JwtEnum {

    /**
     * web 端
     */
    WEB_FRONT(1,
            30 * 24 * 60 * 60 * 1000L,
            "/",
            Constants.TOKEN_HEADER),
    /**
     * 移动 端
     */
    MOBILE_FRONT(2,
            30 * 24 * 60 * 60 * 1000L,
            "/",
            Constants.TOKEN_HEADER),
    ;

    /**
     * app 类型
     */
    private int appType;

    /**
     * token过期时间(毫秒)
     */
    private long expireTime;

    /**
     * 请求路径前缀
     */
    private String reqPrefix;

    /**
     * token key
     */
    private String tokenName;

    JwtEnum(int appType, long expireTime, String reqPrefix, String tokenName) {
        this.appType = appType;
        this.expireTime = expireTime;
        this.reqPrefix = reqPrefix;
        this.tokenName = tokenName;
    }

    /**
     * 根据请求路径获取tokenName
     * @param requestPath
     * @return
     */
    public static JwtEnum getByPath(String requestPath) throws Exception {
        for (JwtEnum jwtEnum : JwtEnum.values()) {
            if (requestPath.startsWith(jwtEnum.getReqPrefix())) {
                return jwtEnum;
            }
        }
        return WEB_FRONT;
    }

    public static JwtEnum getByAppType(int appType) {
        for (JwtEnum e : JwtEnum.values()) {
            if (e.getAppType() == appType) {
                return e;
            }
        }
        return WEB_FRONT;
    }

    public int getAppType() {
        return appType;
    }

    public void setAppType(int appType) {
        this.appType = appType;
    }

    public long getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(long expireTime) {
        this.expireTime = expireTime;
    }

    public String getReqPrefix() {
        return reqPrefix;
    }

    public void setReqPrefix(String reqPrefix) {
        this.reqPrefix = reqPrefix;
    }

    public String getTokenName() {
        return tokenName;
    }

    public void setTokenName(String tokenName) {
        this.tokenName = tokenName;
    }

    /**
     * 生成token的redis的key
     * @param userId
     * @return
     */
    public String generateTokenRedisKey(String userId) {
        return this.getTokenName() + ":" + this.getAppType() + ":" + userId;
    }

}
